package com.zjz.demo.add;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2021/10/08/20:32
 * @Description: 计数器
 * @Target:
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) {

        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("Game Over");
        });

        for (int i = 0; i < 7; i++) {
            // lambda 也就是一个类,
            final int temp = i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"执行"+temp+ "------");

                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }

            },String.valueOf(i)).start();

        }
    }
}
